//生成类型  
//all 全部
//more 增量
//index 只发布首页
//list 只发布列表
//全站发布流程
function publishSite(publishType){
	$("#returnInfo").html("正在发布站点首页....");
	$("#lightsubmit_c").attr("disabled","disabled");
	initPubish();
	var siteId = $("input[name=site.siteId]").val();
	if(publishType=="all"){
		publishType=1;
	}else if(publishType=="more"){
		publishType=2;
	}else if(publishType=="index"){
		publishType=3;
	}else if(publishType=="list"){
		publishType=4;
	}
	$.ajax({	
		url:ctx+"/generate/publish.action?time="+new Date().getTime(),
		data:{"targets":siteId,
			  "type":"site",
			  "pflag":publishType},
		dataType:"text",
		type:"post",
		success:function(data){
			try{
				var json = eval('('+data+')');
				if(json.flag){
					$("input[name=percentKey]").val(json.percentKey);
					returnValue(json.id);
				}else{
					if(json.info!=null){
						alert(json.info);
					}else{
						alert("发布失败！所选站点正在发布！");
					}
				}
			}
			catch (e) {
				var re = /^\{.+\}$/;
				if(!re.test(data)){
					alert("登录超时,请重新登录!");
					window.location.href=window.location.href;					
				}
				else{
					alert("服务器异常");
				}
			}
			
		},
		error:function(data){
			alert("发布遇到错误！点击继续发布下一个任务");
		}
	});
}

function publishCats(catIds,publishType){
	$("#returnInfo").html("正在发布栏目....");
	$("#lightsubmit_c").attr("disabled","disabled");
	initPubish();
	if(publishType=="all"){
		publishType=1;
	}else if(publishType=="more"){
		publishType=2;
	}else if(publishType=="index"){
		publishType=3;
	}else if(publishType=="list"){
		publishType=4;
	}
	$.ajax({	
		url:ctx+"/generate/publish.action?time="+new Date().getTime(),
		data:{"targets":catIds,
			  "type":"cat",
			  "pflag":publishType},
		dataType:"text",
		type:"post",
		success:function(data){
			try{
				var json = eval('('+data+')');
				if(json.flag){
					$("input[name=percentKey]").val(json.percentKey);
					returnValue(json.id);
				}else{
					if(json.info!=null){
						alert(json.info);
					}else{
						alert("发布失败！您无权限发布该栏目或所选栏目站在发布！");
					}
				}
			}
			catch (e) {
				var re = /^\{.+\}$/;
				if(!re.test(data)){
					alert("登录超时,请重新登录!");
					window.location.href=window.location.href;					
				}
				else{
					alert("服务器异常");
				}
			}
			
		},
		error:function(data){
			alert("发布遇到错误！点击继续发布下一个任务");
		}
	});
}

function publishTopics(topicIds,publishType){
	$("#returnInfo").html("正在发布专题....");
	$("#lightsubmit_c").attr("disabled","disabled");
	initPubish();
	if(publishType=="all"){
		publishType=1;
	}else if(publishType=="more"){
		publishType=2;
	}else if(publishType=="index"){
		publishType=3;
	}else if(publishType=="list"){
		publishType=4;
	}
	$.ajax({	
		url:ctx+"/generate/publish.action?time="+new Date().getTime(),
		data:{"targets":topicIds,
			  "type":"topic",
			  "pflag":publishType},
		dataType:"text",
		type:"post",
		success:function(data){
			try{
				var json = eval('('+data+')');
				if(json.flag){
					$("input[name=percentKey]").val(json.percentKey);
					returnValue(json.id);
				}else{
					if(json.info!=null){
						alert(json.info);
					}else{
						alert("发布失败！您无权限发布该专题或改专题正在发布！");
					}
				}
			}
			catch (e) {
				var re = /^\{.+\}$/;
				if(!re.test(data)){
					alert("登录超时,请重新登录!");
					window.location.href=window.location.href;					
				}
				else{
					alert("服务器异常");
				}
			}
		},
		error:function(data){
			alert("发布遇到错误！点击继续发布下一个任务");
		}
	});
}

function publishSurveys(surveyIds,publishType){
	$("#returnInfo").html("正在发布调查....");
	$("#lightsubmit_c").attr("disabled","disabled");
	initPubish();
	if(publishType=="all"){
		publishType=1;
	}else if(publishType=="more"){
		publishType=2;
	}else if(publishType=="index"){
		publishType=3;
	}else if(publishType=="list"){
		publishType=4;
	}
	$.ajax({	
		url:ctx+"/generate/publish.action",
		data:{"targets":surveyIds,
			  "type":"survey",
			  "pflag":publishType},
		dataType:"text",
		type:"post",
		success:function(data){
			try{
				var json = eval('('+data+')');
				if(json.flag){
					returnValue(json.id);
				}else{
					if(json.info!=null){
						$("input[name=percentKey]").val(json.percentKey);
						alert(json.info);
					}else{
						alert("发布失败！所选调查正在发布！");
					}
				}
			}
			catch (e) {
				var re = /^\{.+\}$/;
				if(!re.test(data)){
					alert("登录超时,请重新登录!");
					window.location.href=window.location.href;					
				}
				else{
					alert("服务器异常");
				}
			}
		},
		error:function(data){
			alert("发布遇到错误！点击继续发布下一个任务");
		}
	});
}

function publishAss(){
	$("#returnInfo").html("正在发布理事会信息....");
	$("#lightsubmit_c").attr("disabled","disabled");
	initPubish();
	$.ajax({	
		url:ctx+"/generate/publish.action",
		data:{"type":"associator",
			  "pflag":'1'},
		dataType:"text",
		type:"post",
		success:function(data){
			try{
				var json = eval('('+data+')');
				if(json.flag){
					$("input[name=percentKey]").val(json.percentKey);
					returnValue(json.id);
				}else{
					if(json.info!=null){
						alert(json.info);
					}else{
						alert("发布失败！");
					}
				}
			}
			catch (e) {
				var re = /^\{.+\}$/;
				if(!re.test(data)){
					alert("登录超时,请重新登录!");
					window.location.href=window.location.href;					
				}
				else{
					alert("服务器异常");
				}
			}
		},
		error:function(data){
			alert("发布遇到错误！点击继续发布下一个任务");
		}
	});
}

function returnValue(percentKey){
	var re = /^\d+$/;
	$.ajax({
		url:ctx+"/generate/list-percent.action?time="+new Date().getTime(),
		data:{"percentKey":percentKey},
		dataType:"text",
		type:"post",
		success:function(data){
			var json = eval('('+data+')');
			if(!re.test(json.percent)){
				alert("登录超时,请重新登录!");
				window.location.href=window.location.href;
			}else if(json.percent!='101'){
				$('#pb2').progressBar(json.percent);
				var msg = '';
				var emsg = '';
				if(json.percent!='100'){
					if(json.loginfo==""){
						$("#pia").html(json.loginfo);
					}else{
						for(var i=0;i<json.loginfo.split("@@").length-1;i++){
							if(json.loginfo.split("@@")[i].split("_")[0]=="true"){
								emsg+=json.loginfo.split("@@")[i].split("_")[1]+"\n";
							}
							msg+=json.loginfo.split("@@")[i].split("_")[1]+"\n";
						}
					}
					$("#pia").html(msg);
					$("#pie").html(emsg);
					setTimeout(function(){returnValue(percentKey);},200);
				}else{
					if(json.loginfo==""){
						$("#pia").html(json.loginfo);
					}else{
						for(var i=0;i<json.loginfo.split("@@").length-1;i++){
							if(json.loginfo.split("@@")[i].split("_")[0]=="true"){
								emsg+=json.loginfo.split("@@")[i].split("_")[1]+"\n";
							}
							msg+=json.loginfo.split("@@")[i].split("_")[1]+"\n";
						}
					}
					$("#pia").html(msg);
					$("#pie").html(emsg);
					$("#stoppublish").attr("disabled","disabled");
					$("#lightsubmit_c").attr("disabled","");
				}
			}else{
				$("#lightsubmit_c").attr("disabled","");
				$("#stoppublish").attr("disabled","disabled");
			}
			
		},
		error:function(data) {
			alert("服务器出现异常");
			$("#lightsubmit_c").attr("disabled","");
			$("#stoppublish").attr("disabled","disabled");
		}
	});
}

function stopPublish(){
	$.ajax({	
		url:ctx+"/generate/stop-percent.action?time="+new Date().getTime(),
		data:{"percentKey":$("input[name=percentKey]").val()},
		dataType:"text",
		type:"post",
		success:function(data){
			try{
				var json = eval('('+data+')');
				if(json.flag){
					$("#lightsubmit_c").attr("disabled","");
					$("#stoppublish").attr("disabled","disabled");
				}
			}
			catch (e) {
				var re = /^\{.+\}$/;
				if(!re.test(data)){
					alert("登录超时,请重新登录!");
					window.location.href=window.location.href;					
				}
				else{
					alert("服务器异常");
				}
				
			}
		}
	});
}

function initPubish(){
	$("#pia").html("");
	$("#pie").html("");
	$("#hideInfo").val("隐藏日志");
	$("#changeInfo").val("错误信息");
	$("#publishInfoError").hide();
	$("#publishInfoAll").show();
}
